package com.btex.app;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;
import com.btex.app.adapter.TradeHistoryAdapter;
import com.btex.app.entity.CoinInfo;
import com.btex.app.entity.CoinTradeHistoryInfo;
import com.btex.app.myView.XListView;
import com.btex.app.tools.CookieTool;
import com.btex.app.tools.LogUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wcx on 16/3/24.
 */
public class TradeHistoryActivity extends BaseActivity implements XListView.IXListViewListener, AdapterView.OnItemClickListener {
    private static final java.lang.String TAG = "TradeHistoryActivity";
    private XListView mListView;
    private TradeHistoryAdapter tradeHistoryAdapter;
    private CoinInfo coinInfo;
    private PreferencesCookieStore cookieStore;
    private List<CoinTradeHistoryInfo>  coinTradeHistoryInfos = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_refresh);
        View headerView = View.inflate(cxt,R.layout.activity_header_trade_history,null);
        tradeHistoryAdapter = new TradeHistoryAdapter(cxt);
        coinInfo = (CoinInfo) getIntent().getSerializableExtra("coinInfo");
        cookieStore = CookieTool.getCookieStore(cxt,sp);
        setXListView(headerView);
        initData();
    }

    @Override
    public void setData() {
        tv_title.setText(getString(R.string.trade_history));
        getTradeHistory();
    }

    private void getTradeHistory() {
        RequestParams params = new RequestParams();
        params.addQueryStringParameter("pages", "1");
        params.addQueryStringParameter("pagesize","20");
        params.addQueryStringParameter("pair", coinInfo.getPair());

        OkGo.<String>get(Config.URL_PAIR_HISTORY).params("pages", "1").params("pagesize","20").params("pair", coinInfo.getPair()).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String result = response.body();
                LogUtil.i(TAG, "result:" + result);
                try {
                    JSONObject jsonObj = new JSONObject(result);
                    if ("1".equals(jsonObj.getString(Config.KEY_CODE))) {
                        Type listType = new TypeToken<ArrayList<CoinTradeHistoryInfo>>() {
                        }.getType();
                        coinTradeHistoryInfos = new Gson().fromJson(jsonObj.getJSONObject(Config.KEY_DATA).getString("list"), listType);
                        tradeHistoryAdapter.addData(true, coinTradeHistoryInfos);
                    }
                } catch (Exception e) {
                    LogUtil.i(TAG, e.toString());
                }
                stopRefesh();
            }
        });

//        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.GET, Config.URL_PAIR_HISTORY, params, new RequestCallBack<String>() {
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//                String result = responseInfo.result.toString();
//                LogUtil.i(TAG, "result:" + result);
//                try {
//                    JSONObject jsonObj = new JSONObject(result);
//                    if ("1".equals(jsonObj.getString(Config.KEY_CODE))) {
//                        Type listType = new TypeToken<ArrayList<CoinTradeHistoryInfo>>() {
//                        }.getType();
//                        coinTradeHistoryInfos = new Gson().fromJson(jsonObj.getJSONObject(Config.KEY_DATA).getString("list"), listType);
//                        tradeHistoryAdapter.addData(true, coinTradeHistoryInfos);
//                    }
//                } catch (Exception e) {
//                    LogUtil.i(TAG, e.toString());
//                }
//                stopRefesh();
//            }
//
//            @Override
//            public void onFailure(HttpException error, String msg) {
//                stopRefesh();
//            }
//        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
        }
    }

    /**
     * 初始化Listview
     * @param headerView
     */
    private void setXListView(View headerView) {
        mListView = (XListView)findViewById(R.id.list);
        mListView.setXListViewListener(this);
        mListView.setOnItemClickListener(this);
        mListView.setPullLoadEnable(false);
        mListView.setPullRefreshEnable(true);
        mListView.addHeaderView(headerView);
        mListView.setAdapter(tradeHistoryAdapter);
    }


    public void stopRefesh(){
        mListView.stopRefresh();
        mListView.stopLoadMore();
    }

    @Override
    public void onRefresh() {
        getTradeHistory();
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }


}